package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import db.TratamenduKud;
import db.UpelaKud;

public class Filtratu extends JDialog {
	private Upategia guraso;
	private final JPanel contentPanel = new JPanel();
	private JLabel upelazenb;
	private JTextField upelatxt;
	private JButton gordebtn;
	private JButton atzerabtn;
	private JComboBox comboBox;
	private JLabel upelalitroa;
	private JLabel upelaeduk;
	private JTextField upelarenedukiera;
	private int eduk;

	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			int znb=0, betelitro=0;
			Filtratu dialog = new Filtratu(new Upategia(), MODALA,znb,betelitro);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		public Filtratu(Upategia guraso, Boolean mODALA, final int upelaz, final int betelitro) {
		

			this.guraso = guraso;

			setBounds(200, 200, 480, 300);
			getContentPane().setLayout(new BorderLayout());
			contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
			setTitle("FILTRATU");
			getContentPane().add(contentPanel, BorderLayout.CENTER);
			contentPanel.setLayout(null);
			
			upelazenb= new JLabel("Upela zenbakia:  "+upelaz);
			upelazenb.setBounds(50, 50, 150, 20);
			contentPanel.add(upelazenb);
			
			upelalitroa= new JLabel("Betetako litroak:  "+betelitro);
			upelalitroa.setBounds(250, 50, 180, 20);
			contentPanel.add(upelalitroa);
			
			
			upelazenb= new JLabel("Upela zenbaki berria");
			upelazenb.setBounds(50, 90, 150, 20);
			contentPanel.add(upelazenb);
			
			upelaeduk= new JLabel("Upel berriaren edukiera");
			upelaeduk.setBounds(250, 90, 200, 20);
			contentPanel.add(upelaeduk);
			
			upelarenedukiera = new JTextField();
			upelarenedukiera.setEditable(false);
			upelarenedukiera.setBounds(250, 130, 60, 20);
			contentPanel.add(upelarenedukiera);
			upelarenedukiera.setColumns(10);
			
			comboBox = new JComboBox();
			comboBox.setBounds(100, 130, 50, 20);
			contentPanel.add(comboBox);
			UpelaKud.kargatuComboBox(comboBox);
			comboBox.addActionListener(new ActionListener() {

			
				public void actionPerformed(ActionEvent arg0) {
					String cadena = "";

					int upela = (int) comboBox.getSelectedItem();

					eduk = upelaEdukiera(upela);

					cadena = Integer.toString(eduk);

					upelarenedukiera.setText(cadena);
					
				}
			});
		

	
			gordebtn = new JButton("GORDE");
			gordebtn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
				System.out.println(comboBox.getSelectedIndex());
				
				
					if	(comboBox.getSelectedIndex()==-1){
					JOptionPane.showMessageDialog(Filtratu.this,
							"Aukeratu upel bat !", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
					}
					else {
						int upela= (int) comboBox.getSelectedItem();
					
					 if (eduk<betelitro){ 
					
						JOptionPane.showMessageDialog(Filtratu.this,
								"Upela honetan ez dira hainbeste litro sartzen!", "KONTUZ",
								JOptionPane.WARNING_MESSAGE);

					
					}
					else {
						int konfirmatu = JOptionPane.showConfirmDialog(
								Filtratu.this, "Filtratu nahi duzu?");

						if (JOptionPane.OK_OPTION == konfirmatu) {
							Filtratu.this.aldatu(upela,upelaz);
							Filtratu.this.aldatutratamenduak(upela,upelaz);
							Filtratu.this.guraso.ezabatutaula();
							Filtratu.this.guraso.kargatu();
							Filtratu.this.dispose();
						} else if (JOptionPane.NO_OPTION == konfirmatu) {
							Filtratu.this.dispose();
						}

					}
					}
					
					
				}
			});
			gordebtn.setBounds(80, 170,117, 25);
			contentPanel.add(gordebtn);
			
			atzerabtn = new JButton("UPATEGIA");
			atzerabtn.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					Filtratu.this.dispose();
				}
			});
			atzerabtn.setBounds(250, 170, 117, 25);
			contentPanel.add(atzerabtn);
		}
	
		protected void aldatutratamenduak(int upela, int upelaz) {
			
			TratamenduKud.aldatutratamenduak(upela,upelaz);
		}
		protected void aldatu(int upela, int upelaz) {
			
			UpelaKud.aldatuupela(upela,upelaz);
		}
	private int upelaEdukiera(int n) {
			int edukiera = UpelaKud.lortuUpelarenEdukiera(n);

			return edukiera;
		}
		
}
